package com.zjuee.web.auth.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.bean.response.R;
import com.zjuee.base.constant.Common;
import com.zjuee.base.dto.common.auth.UserDto;
import com.zjuee.base.model.common.auth.User;
import com.zjuee.base.valid.Insert;
import com.zjuee.base.valid.Register;
import com.zjuee.base.valid.Update;
import com.zjuee.service.auth.service.UserService;
import com.zjuee.service.auth.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 用户 web 接口
 *
 * @author wu liang
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping(path = Common.Service.ZJUEE_AUTH_USER_URL_PREFIX, name = Common.Service.ZJUEE_AUTH_SERVICE_NAME)
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 注册 User
     *
     * @param user UserVo
     * @return Long
     */
    @PostMapping("/register")
    public R<Long> register(@Validated(Register.class) @RequestBody UserVo user) {
        try {
            Long add = userService.add(user);
            if (null != add) {
                return R.ok(add);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();
    }

    /**
     * 新增 User
     *
     * @param user UserVo
     * @return Long
     */
    @PostMapping("/add")
    public R<Long> add(@Validated(Insert.class) @RequestBody UserVo user) {
        try {
            Long add = userService.add(user);
            if (null != add) {
                return R.ok(add);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();
    }

    /**
     * 根据 ID 删除 User
     *
     * @param id User Id
     * @return Boolean
     */
    @DeleteMapping("/delete/{id}")
    public R<Boolean> delete(@NotNull @PathVariable(value = "id") Long id) {
//        LoginDto loginDto = (LoginDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        try {
            return userService.delete(id, 0L) ? R.ok() : R.fail();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 修改 User
     * <p>
     * 支  持: Enable,Password
     * 不支持: Name
     *
     * @param user User
     * @return User
     */
    @PutMapping("/update")
    public R<Boolean> update(@Validated(Update.class) @RequestBody UserVo user) {
        try {
            Boolean update = userService.update(user.setName(null));
            if (update) {
                return R.ok(update);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();
    }
    /**
     * 修改密码
     *
     * @param cipherText
     * @return
     */
    @PutMapping("/updatePw")
    public R<Boolean> updatePassword(@NotNull @RequestBody String cipherText) {
        try {
            Boolean update = userService.updatePassword(cipherText);
            if (update) {
                return R.ok(update);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();
    }

    /**
     * 根据 ID 重置 User 密码
     *
     * @param id User Id
     * @return Boolean
     */
    @PostMapping("/rest/{id}")
    public R<Boolean> restPassword(@NotNull @PathVariable(value = "id") Long id) {
        try {
            return userService.restPassword(id) ? R.ok() : R.fail();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 根据 ID 查询 User
     *
     * @param id User Id
     * @return User
     */
    @GetMapping("/info/{id}")
    public R<UserVo> selectById(@NotNull @PathVariable(value = "id") Long id) {
        try {
            UserVo select = userService.selectById(id,0L);
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }

    /**
     * 根据 Name 查询 User
     *
     * @param name User Name
     * @return User
     */
    @GetMapping("/name/{name}")
    public R<User> selectByName(@NotNull @PathVariable(value = "name") String name) {
        try {
            User select = userService.selectByName(name);
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }

    /**
     * 分页查询 User
     *
     * @param userDto Dto
     * @return Page<User>
     */
    @PostMapping("/list")
    public R<Page<UserVo>> list(@RequestBody UserDto userDto) {
        try {
            Page<UserVo> page = userService.list(userDto);
            if (null != page) {
                return R.ok(page);
            }
        } catch (Exception e) {
            log.info(e.getMessage());
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }

    /**
     * 检测用户是否存在
     *
     * @param name User Name
     * @return Boolean
     */
    @GetMapping("/check/{name}")
    public R<Boolean> checkUserValid(@NotNull @PathVariable(value = "name") String name) {
        try {
            return userService.checkUserValid(name) ? R.ok() : R.fail();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }


}
